package com.baizhi.controller;

import com.baizhi.dto.CategoryDTO;
import com.baizhi.po.AddCategoryPO;
import com.baizhi.po.CategoryPO;
import com.baizhi.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类(Category)表控制层
 *
 * @author makejava
 * @since 2021-09-23 20:13:55
 */
@RestController
@RequestMapping("categories")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;  //对字符串支持比较友好,不能存储对象
    @Autowired
    private RedisTemplate redisTemplate;  //存储对象


    @GetMapping
    public List<CategoryPO> queryAll(@RequestParam("token") String token) {
        System.out.println("token = " + token);
        List<CategoryPO> categoryPOS = null;
        if (token != null) {
            HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
            String username = hash.get(token, "username").toString();
            System.out.println("username = " + username);
            if (username != null) {
                categoryPOS = categoryService.queryListByParentId();
            }
        }
        return categoryPOS;
        /*List<CategoryPO> categoryPOS = categoryService.queryListByParentId();
        return categoryPOS;*/
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Integer id, @RequestParam("token") String token) {
        System.out.println("token = " + token);
        if (token != null) {
            HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
            String username = hash.get(token, "username").toString();
            if (username != null) {
                //todo：返回数据
                categoryService.DeleteById(id);
            }
        }

    }

    @PostMapping
    public AddCategoryPO add(@RequestParam("token") String token, @RequestBody CategoryDTO categoryDTO) {
        System.out.println("categoryDTO = " + categoryDTO);

        AddCategoryPO add = null;
        if (token != null) {
            HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
            System.out.println(token);
            //TODO:获取到的数据是空的
            Object username = hash.get(token, "username");
            System.out.println("username" + username);
            if (username != null) {
                add = categoryService.add(categoryDTO);
            }
        }
        return add;
    }

    @PatchMapping("{id}")
    public AddCategoryPO update(@PathVariable("id") Integer id, @RequestBody CategoryDTO categoryDTO, String token) {
        AddCategoryPO update = null;
        if (token != null) {
            HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
            String username = hash.get(token, "username").toString();
            if (username != null) {
                update = categoryService.update(id, categoryDTO);
            }
        }
        return update;
    }

    @GetMapping("queryById/{id}")
    public String queryById(@PathVariable("id") Integer id) {
        String s = categoryService.queryById(id);
        return s;
    }

}

